package com.ja.openchat.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;

import com.google.gson.JsonObject;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.wallet.WalletOrderInfo;

import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

import static com.zy.openchat.Constants.PAGE_COUNT;


public class CollectionRecordViewModel extends PagedViewModel<MultiItemViewModel, WalletOrderInfo> {
    private static final String MULTIRECYCLETYPE_MONTH = "month";
    private static final String MULTIRECYCLETYPE_TOP = "top";
    private static final String MULTIRECYCLETYPE_MIDDLE = "middle";
    private static final String MULTIRECYCLETYPE_BOTTOM = "bottom";

    public CollectionRecordViewModel(@NonNull Application application) {
        super(application);
        //模拟10个条目，数据源可以来自网络
        for (int i = 0; i < 20; i++) {
            if (i == 0) {
                MultiItemViewModel item = new CollectionRecordMonthViewModel(this, "2021年12月1日11:18:19");
                item.multiItemType(MULTIRECYCLETYPE_MONTH);
                observableList.add(item);
            } else {
                String text = "我是第" + i + "条";
                if (i < 6) {
                    if (i == 1) {
                        MultiItemViewModel item = new CollectionRecordTopViewModel(this, text);
                        item.multiItemType(MULTIRECYCLETYPE_TOP);
                        observableList.add(item);
                    } else if (i == 5) {
                        MultiItemViewModel item = new CollectionRecordBottomViewModel(this, text);
                        item.multiItemType(MULTIRECYCLETYPE_BOTTOM);
                        observableList.add(item);
                    } else {
                        MultiItemViewModel item = new CollectionRecordMiddleViewModel(this, text);
                        item.multiItemType(MULTIRECYCLETYPE_MIDDLE);
                        observableList.add(item);
                    }
                } else {
                    if (i == 6) {
                        MultiItemViewModel item = new CollectionRecordMonthViewModel(this, "2021年11月1日11:18:19");
                        item.multiItemType(MULTIRECYCLETYPE_MONTH);
                        observableList.add(item);
                    } else if (i == 7) {
                        MultiItemViewModel item = new CollectionRecordTopViewModel(this, text);
                        item.multiItemType(MULTIRECYCLETYPE_TOP);
                        observableList.add(item);
                    } else if (i == 19) {
                        MultiItemViewModel item = new CollectionRecordBottomViewModel(this, text);
                        item.multiItemType(MULTIRECYCLETYPE_BOTTOM);
                        observableList.add(item);
                    } else {
                        MultiItemViewModel item = new CollectionRecordMiddleViewModel(this, text);
                        item.multiItemType(MULTIRECYCLETYPE_MIDDLE);
                        observableList.add(item);
                    }
                }
            }
        }
    }

    @Override
    public void onMutilItemBind(ItemBinding itemBinding, int position, MultiItemViewModel item) {
        super.onMutilItemBind(itemBinding, position, item);
        //通过item的类型, 动态设置Item加载的布局
        String itemType = (String) item.getItemType();
        if (MULTIRECYCLETYPE_MONTH.equals(itemType)) {
            //设置月份布局
            itemBinding.set(BR.viewModel, R.layout.item_multi_collection_record_month);
        } else if (MULTIRECYCLETYPE_TOP.equals(itemType)) {
            //设置头布局
            itemBinding.set(BR.viewModel, R.layout.item_multi_collection_record_top);
        } else if (MULTIRECYCLETYPE_BOTTOM.equals(itemType)) {
            //设置底布局
            itemBinding.set(BR.viewModel, R.layout.item_multi_collection_record_bottom);
        } else if (MULTIRECYCLETYPE_MIDDLE.equals(itemType)) {
            //设置中间布局
            itemBinding.set(BR.viewModel, R.layout.item_multi_collection_record_middle);
        }
    }

    public void requestData(int page) {
        TelegramManager.getTelegramManager().queryWalletOrderListCall(getApplication(), PAGE_COUNT, currentPage, new TgResultBlock<List<WalletOrderInfo>>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, List<WalletOrderInfo> obj) {
                finishRefreshAndLoad(page, obj, MultiItemViewModel.class);
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                finishRefreshAndLoad(page, null, MultiItemViewModel.class);
            }
        });
    }

    //暂时用假数据，后期接口返回数据直接重写addData方法
    @Override
    public void finishRefreshAndLoad(int page, List<WalletOrderInfo> objects, Class<MultiItemViewModel> t) {
//        super.finishRefreshAndLoad(page, objects, t);
    }

    @Override
    public int initContentView() {
        return 0;
    }
}
